package simple.framework.util;

import simple.framework.util.dbc.*;
import java.util.regex.*;

public class pagi
{
  public conf conf;

  public Long rslimit = 0L;
  public Long rscount = 0L;
  public Long pagenum = 0L;
  public Long pagenums = 0L;
  public Long pagesize = 0L;
  public String sqlstr = "";

  private Long getRsCount()
  {
    Long trscount = 0L;
    String tsqlstr = "select count(*) from " + cls.getLRStr(cls.getLRStr(sqlstr, "from", "rightr"), "order by", "leftr");
    tsqlstr = tsqlstr.trim();
    dbc tDbc = db.newInstance(conf);
    Object[] tArys = tDbc.getDataAry(tsqlstr);
    if (tArys != null)
    {
      Object[][] tAry = (Object[][])tArys[0];
      trscount = cls.getNum64(cls.toString(tDbc.getValue(tAry, 0)));
    }
    return trscount;
  }

  private String getOrderByString1(String argString)
  {
    String tvalue1;
    String tString = argString;
    Pattern tPattern = Pattern.compile("( .[^ ]*\\.)");
    Matcher tMatcher = tPattern.matcher(tString);
    while (tMatcher.find())
    {
      tvalue1 = tMatcher.group(1);
      tString = tString.replace(tvalue1, " ");
    }
    return tString;
  }

  private String getOrderByString2(String argString)
  {
    String tString = argString;
    tString = tString.replace("desc", "_desc");
    tString = tString.replace("asc", "desc");
    tString = tString.replace("_desc", "asc");
    return tString;
  }

  public void Init()
  {
    rscount = getRsCount();

    if (pagesize == 0) pagesize = 20L;

    if (rslimit == 0) rslimit = rscount;
    else
    {
      if (rscount < rslimit) rslimit = rscount;
    }
    if (rslimit % pagesize == 0) pagenums = rslimit / pagesize;
    else pagenums = ((Long)rslimit / pagesize) + 1;
  }

  public Object[] getDataAry()
  {
    Object[] tobject = null;
    if (pagenum == 0) pagenum = 1L;
    Long tpagesize = pagesize;
    Long trslimit = pagesize * pagenum;
    Integer tdbType = cls.getNum(conf.dbtype, 0);
    if (trslimit > rslimit)
    {
      trslimit = rslimit;
      tpagesize = trslimit - (pagesize * (pagenum - 1));
    }
    if (trslimit > 0 && tpagesize > 0)
    {
      String tsqlstr = "";
      if (tdbType >= 0 && tdbType < 10) tsqlstr = "select * from (select * from (" + sqlstr + " limit 0," + trslimit + ") t1 order by " + getOrderByString2(getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"))) + " limit 0," + tpagesize + ") t2 order by " + getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"));
      if (tdbType >= 10 && tdbType < 20) tsqlstr = "select * from (select top " + tpagesize + " * from (select top " + trslimit + cls.getLRStr(sqlstr, "select", "rightr") + ") t1 order by " + getOrderByString2(getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"))) + ") t2 order by " + getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"));
      if (tdbType >= 20 && tdbType < 30) tsqlstr = "select * from (select * from (" + sqlstr + " limit " + trslimit + ") t1 order by " + getOrderByString2(getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"))) + " limit " + tpagesize + ") t2 order by " + getOrderByString1(cls.getLRStr(sqlstr, "order by ", "rightr"));
      if (!cls.isEmpty(tsqlstr))
      {
        dbc tDbc = db.newInstance(conf);
        tobject = tDbc.getDataAry(tsqlstr);
      }
    }
    return tobject;
  }

  public pagi(conf argConf)
  {
    conf = argConf;
  }
}